# -*- coding:utf-8 -*-
"""
@Time : 2022/4/27 5:24 PM
@Author: binkuolo
@Des: 上传图片
"""
import os
import time

from core.Response import success, fail, res_antd

from fastapi import Request, Query, APIRouter, Security, File, UploadFile, Form

from typing import List


router = APIRouter(prefix='/upload')

async  def save_files(reportno,files):
# "\\192.168.1.100\化学\Project case document\2023年照片"
# 得到年月日, 写入路径中
    now = time.localtime()
    year,month,day,hour,minute,second = now.tm_year,now.tm_mon,now.tm_mday, \
                                        now.tm_hour,now.tm_min,now.tm_sec
    print(year,month,day,hour,minute,second)
    year = str(year)
    month = str(month)
    day=str(day)
    path1 = '\\\\192.168.1.100\\化学\\Project case document\\'+year+'年照片'+'\\'+month+'月份'+'\\'+day+'号'
    #     2023年照片    3月份    24号
    #      year     month     day
    if not os.path.exists(path1+'\\'+reportno):
        import shutil

        # shutil.rmtree(path1+'\\'+reportno)

        # os.remove(path1+'\\'+reportno)

        os.makedirs(path1 + '\\' + reportno)
    print(path1+'\\'+reportno)
    for file in files:
        "type:<class 'coroutine'>"
        cont = await file.read()

        with open(path1+'\\'+reportno+'\\'+file.filename,'ab') as f:
            f.write(cont)
    return success(msg="新增成功")

#year:str= Form(...),month:str= Form(...),day:str= Form(...),
@router.post("/",  summary="上传图片")
async def upload(reportno: str = Form(...),files: List[UploadFile] = File(...)):
    print(reportno)
    # reportno='A230'
    result =   await save_files(reportno,files)
    if not result:
        return fail(msg="创建失败!")
    return success(msg="创建成功!")

